#include <stdio.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <sys/select.h>


int main(int argc, const char *argv[])
{
	fd_set readfds;
	FD_ZERO(&readfds);

	int sockfd=socket(AF_INET,SOCK_STREAM,0);
	struct sockaddr_in serveraddr,clientaddr;

	serveraddr.sin_addr.s_addr=inet_addr(argv[1]);
	serveraddr.sin_family=AF_INET;
	serveraddr.sin_port=htons(atoi(argv[2]));
	int num=0;
	socklen_t len=sizeof(serveraddr);

	bind(sockfd,(struct sockaddr *)&serveraddr,len);
	listen(sockfd,5);
	char inputbuf[32]={0};
	while(1)
	{
		FD_SET(0,&readfds);	
		FD_SET(sockfd,&readfds);
		select(sockfd+1,&readfds,NULL,NULL,NULL);
		if(FD_ISSET(0,&readfds)==1)
		{
			fgets(inputbuf,32,stdin);
			printf("your input :%s\n",inputbuf);
			memset(inputbuf,0,32);
		}
		if(FD_ISSET(sockfd,&readfds)==1)
		{
			int acceptfd=accept(sockfd,(struct sockaddr *)&clientaddr,&len);
			printf("client connect %d!\n",num++);
		}
	}
	return 0;
}
